import 'dart:async';
import 'dart:core';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:picker_select/city_picker/base.dart';
import 'package:picker_select/city_picker/index.dart';
import 'package:picker_select/city_picker/picker_popup_route.dart';

import './util.dart';
import 'model/result.dart';
import 'show_types.dart';
import 'meta/province.dart' as meta;

/// ios city pickers
/// provide config height, initLocation and so on
///
/// Sample:flutter format
/// ```
/// await CityPicker.showPicker(
///   location: String,
///   height: double
/// );
///
/// ```

class CityPickers {
  /// static original city data for this plugin
  static Map<String, dynamic> metaCities = meta.citiesData;

  /// static original province data for this plugin
  static Map<String, dynamic> metaProvinces = meta.provincesData;

  static utils(
      {Map<String, dynamic> provinceData, Map<String, dynamic> citiesData}) {
    print("CityPickers.metaProvinces::: ${CityPickers.metaCities}");
    return CityPickerUtil(
      provincesData: provinceData ?? CityPickers.metaProvinces,
      citiesData: citiesData ?? CityPickers.metaCities,
    );
  }

  /// use
  /// @param context BuildContext for navigator
  /// @param locationCode initial select, one of province area or city id
  ///                 if given id is provinceId, the city and area id will be this province's first city and first area in metadata
  /// @param height Container's height
  ///
  /// @param Theme used it's primaryColor
  ///
  /// @param barrierDismissible whether user can dismiss the modal by touch background
  ///
  /// @param cancelWidget customer widget for building cancel button
  ///
  /// @param confirmWidget customer widget for building confirm button
  ///
  /// @param itemBuilder customer widget for building item
  ///
  /// @return Result see [Result]
  ///
  static Future<Result> showCityPicker(
      {@required BuildContext context,
      showType = ShowType.pca,
      double height = 350.0,
      String locationCode = '110000',
      ThemeData theme,
      Map<String, dynamic> citiesData,
      Map<String, dynamic> provincesData,
      // CityPickerRoute params
      bool barrierDismissible = true,
      double barrierOpacity = 0.5,
      ItemWidgetBuilder itemBuilder,
      double itemExtent,
      Widget cancelWidget,
      Widget confirmWidget,
      bool isSort = false}) {
    return Navigator.of(context, rootNavigator: true).push(
      new CityPickerRoute(
          theme: theme ?? Theme.of(context),
          canBarrierDismiss: barrierDismissible,
          barrierOpacity: barrierOpacity,
          barrierLabel:
              MaterialLocalizations.of(context).modalBarrierDismissLabel,
          child: BaseView(
              isSort: isSort,
              showType: showType,
              height: height,
              itemExtent: itemExtent,
              itemBuilder: itemBuilder,
              cancelWidget: cancelWidget,
              confirmWidget: confirmWidget,
              citiesData: citiesData ?? meta.citiesData,
              provincesData: provincesData ?? meta.provincesData,
              locationCode: locationCode)),
    );
  }
}
